<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
	<jsp:include page="/include.jsp"/>
	<script type="text/javascript">
	
	$(function(){
		//新增按纽绑定事件
		$('#add').bind('click',doAdd);
		
		//查询表格数据
		getDatagridByID();
	});
	
	//查询数据方法
	function getDatagridByID()
	{
		$("#templateTab").datagrid({
			url : '${pageContext.request.contextPath}/basemgr/templateAction!getTemplateByID.action',
			queryParams:serializeObject($('#search_form')),
			fit:true,
			border:false,
			fitColumns:true,
			pagination:true,
			pageList:[10,20,30],
			rownumbers:true,
			singleSelect:false,
			pagePosition:'bottom',
			autoRowHeight:true,
			striped:true,
			columns:[[  {field:'templateId',checkbox:true},
			            {field:'templateName',title:'模板名称',width:60,align:'left',formatter:function(value,rowData,rowIndex){
			                return '<div title="'+rowData.templateName+'" onmouseover="onMouseOverName(this)"  onmouseout="onMouseOutName(this)" onclick="templateDetial('+rowData.templateId+')"  style="color:#2b76b7;cursor:pointer;width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">'+rowData.templateName+'</div>';
			            }},
						{field:'templateContent',title:'模板内容',width:60,align:'center',formatter:function(value,rowData,rowIndex){
			            	if(rowData.templateContent!=undefined){
			            		  return '<div style="width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="'+rowData.templateContent+'">'+rowData.templateContent+'</div>';
			            		  }

								}},
						{field:'creater',title:'创建人',width:40,align:'center'},
						{field:'modifier',title:'修改人',width:40,align:'center'},
						{field:'dsafd',title:'操作',width:40,align:'center',formatter:function(value,rowData,rowIndex){
							return '<span  style="cursor:pointer" onclick="editTemplate('+rowData.templateId+')"><img  src="${pageContext.request.contextPath}/images/icons/modify_or_edit.png" title="修改" alt="修改"/>修改&nbsp;&nbsp;</span>'
								  +'<span  style="cursor:pointer" onclick="deleteTemplate('+rowData.templateId+')"><img src="${pageContext.request.contextPath}/images/icons/delete.png" title="删除" alt="删除"/>删除</span>';
							
						}}
						
					]],
			toolbar:'#template_toobar'
		});
	}
	
	//分页查询
	function doQuery()
	{
		var dataGridOptions=$('#templateTab').datagrid('options');
		dataGridOptions.queryParams=serializeObject($('#search_form'));
		$('#templateTab').datagrid(dataGridOptions);
		$('#templateTab').datagrid('load');
	}

	
	//修改模板
	function editTemplate(templateId){		
        var pars="templateId='"+templateId+"'";
        $.ajax({
			url :'${pageContext.request.contextPath}/basemgr/templateAction!getUsedSeveral.action',
			data:pars,
			dataType :'json',
			success : function(usedResult){ 
			    if(usedResult.returnFlag){
			    	$.messager.show({
						title:'提示',
						msg:usedResult.returnMsg
					}); 
			    } else {
			    	pars="templateId="+templateId;
			    	window.top.$('<div/>').dialog({
						href:'${pageContext.request.contextPath}/basemgr/templateAction!getTemplateDetailByID.action?'+pars,
						top:($(window).height()-80),
					    left:220,
						width:871,
						height:560,
						modal:true,
						title:'修改模板',
						buttons:[{
							text:'保 存',
							iconCls:'',
							handler:function(){
								var d=window.top.$(this).closest('.window-body');
								 var addFlag=window.top.draggableData();//获取模板内容
								 if(addFlag){
									 window.top.$('#template_edit_form').form('submit',{
											url:'${pageContext.request.contextPath}/basemgr/templateAction!updateTemplate.action',
											success:function(result){
												try {
													var r = $.parseJSON(result);
													$.messager.show({
														title : '提示',
														msg : r.returnMsg
													});
													if (r.returnFlag) {
														d.dialog('destroy');
														$('#search_form').form('clear');
														getDatagridByID();
													}
													
												} catch (e) {
													$.messager.alert('提示', result);
												}
												}
											});
								 }
								}
							}],
						onClose:function(){
							window.top.$(this).dialog('destroy');
						}
					});
			        
			    }
			}
        });
        
	}
	
	//查看模板
	function templateDetial(templateId){		
        var pars="templateId="+templateId;
        window.top.$('<div/>').dialog({
			href:'${pageContext.request.contextPath}/basemgr/templateAction!getTemplateDetail.action?'+pars,
			top:($(window).height()-80),
		    left:220,
			width:871,
			height:550,
			modal:true,
			title:'查看模板详细',
			onClose:function(){
				window.top.$(this).dialog('destroy');
			}
		});
        
	}
	//单个模板删除
	function deleteTemplate(templateId){
        var pars="templateId='"+templateId+"'";
        // 判断模板是否能被删除，如果模板已经被引用，则不能删除
        $.ajax({
			url :'${pageContext.request.contextPath}/basemgr/templateAction!getUsedSeveral.action',
			data:pars,
			dataType :'json',
			success : function(usedResult){ 
			    if(usedResult.returnFlag){
			        $.messager.show({
						title:'提示',
						msg:usedResult.returnMsg
					});
			    } else {
			    	$.messager.confirm('删除确认', '确定要删除该模板吗？',function(r){
						if(r){
							$.ajax({
					        	url :'${pageContext.request.contextPath}/basemgr/templateAction!deleteTemplate.action',
					        	data:pars,
					        	dataType :'json',
					        	success : function(r1){ 
					        		$.messager.show({
					        			title:'提示',
					        			msg:r1.returnMsg,
					        			showType:'slide'
					        		});
					        	}
					        });
					        $('#search_form').form('clear');
					        getDatagridByID();
						}
					});
			        
			    }
			}
        });
	}
	
	
	//批量删除
	function deleteTemplates(){
		var templateId="";
		var obj=$("#templateTab").datagrid("getChecked");
        if(null!=obj){
        	for(var idx=0;idx<obj.length;idx++){        		
        		templateId+="'"+obj[idx].templateId+"',";
        	}
        	templateId=templateId.substring(0,templateId.length-1);
            if(templateId==''){
            	window.top.$.messager.alert('提示','没有选择任何记录');
            	return;
            }  	
        }else{
        	window.top.$.messager.alert('提示','没有选择任何记录');
        	return;
        }
        var pars="templateId="+templateId;
        // 判断模板是否能被删除，如果模板已经被引用，则不能删除
        $.ajax({
			url :'${pageContext.request.contextPath}/basemgr/templateAction!getUsedSeveral.action',
			data:pars,
			dataType :'json',
			success : function(usedResult){ 
			    if(usedResult.returnFlag){
			        $.messager.show({
						title:'提示',
						msg:usedResult.returnMsg
					});
			    } else {
			    	$.messager.confirm('删除确认', '确定要删除该些模板吗？',function(r){
						if(r){
							$.ajax({
					        	url :'${pageContext.request.contextPath}/basemgr/templateAction!deleteTemplate.action',
					        	data:pars,
					        	dataType :'json',
					        	success : function(r1){ 
						        	$.messager.show({
						        		title:'提示',
										msg:r1.returnMsg,
										showType:'slide'
									});
								}
							});
							$('#search_form').form('clear');
							getDatagridByID();
						}
					});
			        
			    }
			}
		});
	}
	//新增
    function doAdd(){
    	window.top.$('<div/>').dialog({
			href:'${pageContext.request.contextPath}/dqm/basemgr/template/template_add.jsp',
			top:($(window).height()-80),
		    left:220,
			width:871,
			height:560,
			modal:true,
			title:'新增模板',
			buttons : [ {
				id:'btnSave',
				text:'保 存',
				iconCls:'',
				handler : function() {
					var d = window.top.$(this).closest('.window-body');
					var templateName=window.top.$("#templateName_add").val();
					 if(""!=templateName && templateName.length>0){
						 $.ajax({
							 url :'${pageContext.request.contextPath}/basemgr/templateAction!validTemplateName.action',
							 data:{"templateName":templateName},
							 dataType :'json',
							 cache : true,
							 success : function(data){
								if(data.returnFlag){
									 var str="*模板名称已存在*";
									 window.top.$("#template_main_templateName").html(str);
								}else{
									window.top.$("#template_main_templateName").html("");
									 var addFlag=window.top.draggableData();//获取模板内容
									 if(addFlag){
										window.top.$('#template_main_form').form('submit', {
											url : '${pageContext.request.contextPath}/basemgr/templateAction!addTemplate.action',
											success : function(result) {
												try {
													var r = $.parseJSON(result);
													$.messager.show({
														title : '提示',
														msg : r.returnMsg
													});
													if (r.returnFlag) {
														d.dialog('destroy');
														$('#search_form').form('clear');
														getDatagridByID();
													}
												} catch (e) {
													$.messager.alert('提示', result);
												}
											}
										});
									}
								}
							 }
					     });
					 }else{
						 window.top.$('#template_main_form').form('submit', {
								url : '${pageContext.request.contextPath}/basemgr/templateAction!addTemplate.action',
								success : function(result) {
									try {
										var r = $.parseJSON(result);
										$.messager.show({
											title : '提示',
											msg : r.returnMsg
										});
										if (r.returnFlag) {
											d.dialog('destroy');
											$('#search_form').form('clear');
											getDatagridByID();
										}
									} catch (e) {
										$.messager.alert('提示', result);
									}
								}
							});
					 }
				}
			} ],
			onClose:function(){
				window.top.$(this).dialog('destroy');
			}
		});
    }
	
    
    //重置方法
    function resetData(){
    	$("#templateName").val('');
    	$('#templateContent').val('');
    }
	</script>
</head>

<body class="easyui-layout">
    <form id="search_form" method="post">
        <div data-options="region:'north'" style="height:80px;border-bottom:none;border-top:none;">
	    	<table border="0" cellpadding="0" cellspacing="0" height="100%"  width="100%" >
					<tr>
						<td style="width:10%;text-align:right;" nowrap="nowrap">
							<label for="reportnameQ">模板名称：</label>
						</td>
						<td style="width:15%;text-align:right;" nowrap="nowrap">
							<input name="templateName" type="text" id="templateName" class="easyui-validatebox" type="text"/> 
						</td>
						<td style="width:10%;text-align:right;" nowrap="nowrap">
							<label for="reportnameQ">模板内容：</label>
						</td>
						<td style="width:15%;text-align:right;" nowrap="nowrap">
							<input id="templateContent" type="text" class="easyui-validatebox" panelHeight='auto' name="templateContent" />
						</td>					
						<td colspan="2" style="width:25%;text-align:right;" nowrap="nowrap">	   
				    		<input type="button" onclick="doQuery()" value="查询" style="cursor:pointer;background-image: url('${pageContext.request.contextPath}/images/login/search.png');background-repeat: no-repeat;width:67px;height:23px;padding-left:12px;border:none;color:white;font-family:微软雅黑"/>	
				    		<input type="button" onclick="resetData()" value="重置" style="cursor:pointer;background-image: url('${pageContext.request.contextPath}/images/login/reset.png');background-repeat: no-repeat;width:67px;height:23px;padding-left:12px;border:none;color:white;font-family:微软雅黑;margin-right:35px"/>			
		    			</td>
		    			<td style="width:10%;text-align:right;" nowrap="nowrap">
						</td>
						<td style="width:15%;text-align:right;" nowrap="nowrap">
						</td>		
					</tr>
			</table>
	    </div>  
	    <div data-options="region:'center'" style="border-top:none">  
			<table id="templateTab"></table>		
			<div id="template_toobar" class="etl_panel_title" >
				<a  id="add" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" style="float:left">新 增</a>
				<div class="datagrid-btn-separator"></div>
				<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true"  onclick="deleteTemplates()">批量删除</a>
			</div>
	   </div>
    </form>
</body>
</html>
